//
//  YKFarmDrugAddController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/12.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmDrugAddController.h"
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>

@interface YKFarmDrugAddController ()
///
@property(nonatomic,strong) NSMutableDictionary *params;
///
@property(nonatomic,copy) void(^comfirmBlock)(NSDictionary *params);
///
@property(nonatomic,strong) UITextField *drugnameTextField;
///
@property(nonatomic,strong) UITextField *drugmuchTextField;

@end

@implementation YKFarmDrugAddController

- (instancetype)initWithFarmId:(NSString *)farmId comfirmBlock:(void(^)(NSDictionary *params))block
{
    if (self = [super init]) {
        self.title = @"喂药记录";
        self.params = [@{@"farmId":farmId} mutableCopy];
        self.comfirmBlock = block;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *topView = UIView.instance.yk_borderColor(UIColor.blackColor);
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    UILabel *titleLabel = UILabel.instance;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont systemFontOfSize:20];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView.mas_centerY);
        make.centerX.mas_equalTo(topView.mas_centerX);
    }];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    UIButton *comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [comfirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [comfirmButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:comfirmButton];
    [comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    
    UILabel *drugname = UILabel.instance;
    drugname.text = @"药名:";
    drugname.font = [UIFont systemFontOfSize:20];
    drugname.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:drugname];
    [drugname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(10);
        make.top.mas_equalTo(topView.mas_bottom).offset(10);
        make.width.mas_equalTo((YKScreenW - 20)/2);
        make.height.mas_equalTo(24);
    }];
    [self.view addSubview:self.drugnameTextField];
    [self.drugnameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(drugname.mas_top);
        make.left.mas_equalTo(drugname.mas_right);
        make.right.mas_equalTo(self.view.mas_right).offset(-10);
        make.width.mas_equalTo(drugname.mas_width);
        make.height.mas_equalTo(drugname.mas_height);
    }];
    
    UILabel *drugmuch = UILabel.instance;
    drugmuch.text = @"药量:";
    drugmuch.font = [UIFont systemFontOfSize:20];
    drugmuch.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:drugmuch];
    [drugmuch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(drugname.mas_bottom).offset(5);
        make.left.width.height.mas_equalTo(drugname);
    }];
    [self.view addSubview:self.drugmuchTextField];
    [self.drugmuchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(drugmuch);
        make.right.mas_equalTo(self.drugnameTextField.mas_right);
    }];
    
    @weakify(self);
    [[cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [[comfirmButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.comfirmBlock) {
            
            NSString *date;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            [fmt setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
            date = [fmt stringFromDate:[NSDate date]];
            NSString *str = [NSString stringWithFormat:@"%@",date];
            self.params[@"createTime"] =  str;
            self.params[@"drugname"] = self.drugnameTextField.text;
            self.params[@"much"] = self.drugmuchTextField.text;
            self.params[@"MealTime"] = @"";
            self.comfirmBlock(self.params);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
    [self.view yk_tapOnView:^(UITapGestureRecognizer *tap) {
        @strongify(self);
        [self resignFirstResponder];
    }];
}

- (UITextField *)drugnameTextField
{
    if (!_drugnameTextField) {
        _drugnameTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
    }
    return _drugnameTextField;
}

- (UITextField *)drugmuchTextField
{
    if (!_drugmuchTextField) {
        _drugmuchTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
        _drugmuchTextField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _drugmuchTextField;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
